package model;

import java.util.ArrayList;

import com.mysql.jdbc.Connection;

import dao.Access;
import dao.Database;
import entities.Jugador;

public class AccessManager {
	
	int player1Id = 1;
	int player2Id = 2;
	public static boolean player1Here = false;
	public static Jugador player1 = new Jugador(1, 10, 10);
	public static Jugador player2= new Jugador(2, 20, 20);
	
	public boolean isPlayer1Connected(){
		return player1Here;
	}
	
	public int getMyId(){
		if(player1Here == false)
			return player1Id;
		else
			return player2Id;
	}
	
	public boolean updatePos(int id, float x, float y){
		if(id == 1){
			player1.setCoordenadaX(x);
			player1.setCoordenadaY(y);
		}else{
			player2.setCoordenadaX(x);
			player2.setCoordenadaY(y);
		}
		return true;
	}
	
	public Jugador getEnemy(int id){
		System.out.println("PIDO ENEMIGO");
		if(id == 1){
			return player2;
		}else{
			return player1;
		}
	}

	public ArrayList<Jugador> getJugadores() throws Exception
	{
		ArrayList<Jugador> jugadores = new ArrayList<Jugador>();
		Database db = new Database();
		Connection con = db.getConnection();
		Access access = new Access();
		jugadores = access.getJugadores(con);
		return jugadores;
	}
	
	public Jugador getUltimaposicion() throws Exception
	{	Jugador j = new Jugador();
		Database db = new Database();
		Connection con = db.getConnection();
		Access access = new Access();
		j = access.getUltimaposicion(con);
		if(player1Here == false){
			player1Here = true;
			System.out.println("PRIMERA VEZ: ID 1");
			j.setId(1);
		}else{
			j.setId(2);
			System.out.println("PRIMERA VEZ: ID 2");
		}

		return j;
	}
	
	public boolean agregarJugador(float pCoordX, float pCoordY) throws Exception
	{
		Database db = new Database();
		Connection con = db.getConnection();
		Access access = new Access();
		System.out.println("GUARDANDO");
		return access.agregarJugador(con, pCoordX, pCoordY);
		
	
	}
}
